<template>
  <div class="login">
    <div class="main">
      <!-- 标题图片 -->
      <img src="@/assets/common/login-logo.png" alt="" class="img">
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 手机号 -->
        <el-form-item prop="mobile">
          <!-- 密码框回车事件也可以验证全局表单 -->
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <!-- show-password表示密码框的眼睛 -->
          <!-- native修饰符可以让组件使用原生方法 -->
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button
            class="login"
            :loading="loading"
            @click="submit"
          >{{ $t('l') }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      // 设置表单验证数据
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 表单验证规则
      rules: {
        mobile: [
          { required: true, message: '手机号必填', trigger: 'change' },
          { min: 11, max: 11, message: '手机号码必须是11位', trigger: 'blur' },
          // 自定义全局表单验证
          {
            validator: (rules, value, callback) => {
              // 判断输入的手机号码是否正确
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('输入手机号不正确'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '密码框必填', trigger: 'change' },
          { min: 6, max: 11, message: '密码要在6-11位字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 全局表单验证方法：validate，在methods方法内
    // this.$refs.form.validate可以获取子组件的验证方法
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 验证成功发送登录请求
          // this.loading = true
          try {
            await this.$store.dispatch('user/toLogin', this.form)
            // this.loading = false
            // 跳转到首页
            // 点击登录的时候获取当前路由的路径信息fullpath
            const path = this.$route.query.redirect || '/'
            this.$router.push(path)
          } catch {
            // this.loading = false
          }
        } else {
          alert('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  ::v-deep .el-input__inner {
    height: 50px;
  }
  .img {
    margin-bottom: 20px;
  }
  .main {
    width: 580px;
    height: 300px;
    .login {
      width: 100%;
      background-color: rgb(33, 103, 251);
    }
    .el-button {
      border: 0;
      color: #fff;
    }
  }
}
</style>
